package edu.uoc.tds.pac4.client;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import edu.uoc.tds.pac4.client.Client;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.beans.Administrador;
import edu.uoc.tds.pac4.beans.Participant;
import edu.uoc.tds.pac4.beans.Secretaria;
import edu.uoc.tds.pac4.beans.Usuari;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

/**
 * Pantalla que permet canviar la contrasenya d'un usuari
 */
public class PantallaCanviClau implements PanelClientInterface {

	private static final long serialVersionUID = 1L;
	private JPanel jPanelContainer = null;
	//private JPanelFiltreCalendari jPanelFiltre = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField jUserField = new JTextField();
	private JPasswordField jPasswordField = new JPasswordField();
	// Verificacio Clau
	private JPasswordField jPasswordField2 = new JPasswordField();
	private JButton jButtonCancelar = null;
	private JLabel jLabelImage = null;
	private JLabel jLabelText = null;
	protected Usuari usuari = null;
	protected RemoteInterface remot;
	private Client client;
	private String userId;

	/**
	 * Constructor
	 */
	public PantallaCanviClau() {
		super();
		initialize();
	}

	/**
	 * Constructor que rep una referencia al servidor remot
	 * @param client 
	 * 
	 * @param remot
	 */
	public PantallaCanviClau(Client client, RemoteInterface remot) {
		this.remot = remot;
		this.client = client;
		initialize();
	}

	/**
	 * Inicialitza la pantalla
	 */
	protected void initialize() {
		usuari = client.getUserConnected();
		userId = usuari.getCodi();
	}

	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.ipadx = 530;
			gridBagConstraints.ipady = 250;
			gridBagConstraints.gridy = 0;
			jPanelContainer = new JPanel();
			jPanelContainer.setSize(dimension);
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanel(), gridBagConstraints);
		}
		return jPanelContainer;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabelText = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaCanviClau.Title"));
			jLabelText.setBounds(new Rectangle(160, 15, 230, 30));
			jLabelText.setFont(new Font("Arial", Font.PLAIN, 21));
			jLabelText.setForeground(new Color(0x8B008B));
			ImageIcon imagenExterna = new ImageIcon(this.getClass().getResource("/img/login.png"));
			jLabelImage = new JLabel(imagenExterna);
			jLabelImage.setBounds(new Rectangle(10, 40, 128, 128));
			jLabel1 = new JLabel();
			jLabel1.setText(TDSLanguageUtils.getMessage("Client.PantallaIdentificacio.User"));
			jLabel1.setBounds(new Rectangle(150, 80, 150, 25));
			jUserField.setBounds(new Rectangle(300, 80, 200, 25));
			jUserField.setText(usuari.getCodi());
			jUserField.setEnabled(false);
			jLabel = new JLabel();
			jLabel.setText(TDSLanguageUtils.getMessage("Client.PantallaCanviClau.Clau"));
			jLabel.setBounds(new Rectangle(150, 125, 150, 25));			
			jPasswordField.setEchoChar('*');
			jPasswordField.setBounds(new Rectangle(300, 125, 200, 25));
			jLabel2 = new JLabel();
			jLabel2.setText(TDSLanguageUtils.getMessage("Client.PantallaCanviClau.Clau2"));
			jLabel2.setBounds(new Rectangle(150, 170, 150, 25));
			jPasswordField2.setEchoChar('*');
			jPasswordField2.setBounds(new Rectangle(300, 170, 200, 25));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(getJButtonAcceptar(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(jUserField, null);
			jPanel.add(jPasswordField, null);
			jPanel.add(jLabel2, null);
			jPanel.add(jPasswordField2, null);
			jPanel.add(getJButtonCancelar(), null);
			jPanel.add(jLabelImage, null);
			jPanel.add(jLabelText, null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAcceptar() {

		if (jButton == null) {
			jButton = new JButton("Acceptar");
			jButton.setBounds(new Rectangle(85, 215, 100, 25));
			jButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){		
					try {
						changeClauUsuari();
					} catch (Exception e){
						new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
					} 
				}		
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButtonCancelar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton("Cancelar");
			jButtonCancelar.setBounds(new Rectangle(240, 215, 100, 25));
			jButtonCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0){
					jPasswordField.setText("");
					jPasswordField2.setText("");
					jPasswordField.requestFocus();
				}
			});
		}
		return jButtonCancelar;
	}

	private void changeClauUsuari() {
		char[] c = jPasswordField.getPassword();
		String clau = new String(c);
		char[] c2 = jPasswordField.getPassword();
		String clau2 = new String(c2);
		try {

			if (clau2.equals(clau)){				
				if(clau.equals("")){
					new PantallaError("Info_canvi_clau_wrong", PantallaError.Tipus.INFO);
					client.netejarPantalla();
				} else {
					//Canviar clau segons perfil
					String perfil = usuari.getPerfilUsuari();
					if (perfil.equals("Administrador")){
						Administrador a = remot.getAdministrador(userId);
						a.setClau(clau2);
						remot.update(a);						
					} else {
						if (perfil.equals("Secretaria")){
							Secretaria s = remot.getSecretaria(userId);
							s.setClau(clau2);
							remot.update(s);							
						} else {
							if (perfil.equals("Participant")){
								Participant part = remot.getParticipant(userId);
								part.setClau(clau2);
								remot.update(part);								
							}
						}
					}
					new PantallaError("Info_canvi_clau_OK", PantallaError.Tipus.INFO);
					client.netejarPantalla();
				}
			} else {
				new PantallaError("Error_canvi_clau", PantallaError.Tipus.ERROR);

			}
		} catch (Exception e){
			new PantallaError("Error_check_user", PantallaError.Tipus.ERROR);
		}
	}
}